package com.mianyang.zhangsan.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mianyang.zhangsan.common.util.OperationManHelper;
import com.mianyang.zhangsan.common.util.StringUtils;
import com.mianyang.zhangsan.model.dto.SecAccountRole;
import com.mianyang.zhangsan.model.entity.SecAccount;
import com.mianyang.zhangsan.mapper.SecAccountMapper;
import com.mianyang.zhangsan.model.entity.SecAuthority;
import com.mianyang.zhangsan.model.entity.SecRole;
import com.mianyang.zhangsan.service.ISecAccountService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.mianyang.zhangsan.service.ISecAuthorityService;
import com.mianyang.zhangsan.service.ISecRoleService;
import lombok.NonNull;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author jiangjr
 * @since 2022-10-23
 */
@Service
public class SecAccountServiceImpl extends ServiceImpl<SecAccountMapper, SecAccount> implements ISecAccountService {
    @Autowired
    private ISecRoleService roleService;

    @Override
    public SecAccount selectAccountByUsername(String username) {
        QueryWrapper<SecAccount> wrapper = new QueryWrapper<>();
        if (!StringUtils.isEmpty(username)) {
            wrapper.eq("account_name", username);
            return baseMapper.selectOne(wrapper);
        }
        return null;
    }

    @Override
    public List<SecAccount> selectLowerAccount(@NonNull Long userId) {
        QueryWrapper<SecAccount> wrapper = new QueryWrapper<>();
        wrapper.eq("parent_id", userId);
        // 查询下级用户
        List<SecAccount> list = list(wrapper);
        list.forEach(ac -> ac.setAccountWord(null));
        return list;
    }

    @Override
    public SecAccount selectSelfAccount(@NonNull Long userId) {
        SecAccount account = getById(userId);
        account.setAccountWord(null);
        return account;
    }

    @Override
    public void updateAccount(SecAccount account) {
        baseMapper.updateById(account);
    }

    @Override
    public List<SecAuthority> selectCurrentAccountAuthority() {
        Long accountId = OperationManHelper.getAccountId();
        return roleService.selectAuthority(accountId);
    }

    @Override
    public List<SecAuthority> selectCurrentAccountAuthority(Long accountId) {
        return roleService.selectAuthority(accountId);
    }

    @Override
    public Page<SecAccount> selectAccounts(SecAccount account, int current, int pageSize) {
        QueryWrapper<SecAccount> wrapper = new QueryWrapper<>();
        if (StringUtils.isNotEmpty(account.getAccountName())) {
            wrapper.like("account_name", account.getAccountName());
        }
        Page<SecAccount> page = page(new Page<>(current, pageSize), wrapper);
        List<SecAccount> records = page.getRecords();
        records.forEach(ac -> ac.setAccountWord(null));
        page.setRecords(records);
        return page;
    }

    @Override
    public SecAccountRole selectAccountRoles(Long accountId) {
        return roleService.selectAccountRoles(accountId);
    }
}
